package algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import algorithm.domain.Point;
import algorithm.search.SearchSYT;
import algorithm.sort.SortSYT;

public class UtilSYT {
	public static List<Point> SearchSum(int[] a, int sum){
		Arrays.sort(a);
		List<Point> pointList = new ArrayList<Point>();
		int j;
		int y;
		int index;
		int l = a.length;
		for(int i = 0; i < a.length - 1; i++ ){
			y = sum - a[i];
			index = Arrays.binarySearch(a,i+1, l-1, y);
			if(index < 0)
				continue;
			
			pointList.add(new Point(i,index));
			j = index;
			while(++j < l && a[j] == a[j-1]){
				System.out.println(j);
				pointList.add(new Point(i,j));
			}
		}
		return pointList;
	}
	
	public static void printArr(int[] a){
		String s = "\n";
		for(int i = 0; i < a.length; i++){
			s  = s + " " + a[i];
		}
		System.out.println(s);
	}
	
	public static void printPointArr(int[] a, List<Point> pointList){
		String s = "\n";
		for(Point point:pointList){
			s  = s + "index_" + point.getX() + "_value_" + a[point.getX()] + "-index_" + point.getY() + "_value_" + a[point.getY()] + "\n";
		}
		System.out.println(s);
	}
	public static void main(String[] args) {
		int[] a = new int[100];
		Random random = new Random();
		for(int i = 0; i< 100; i++){
			a[i] = random.nextInt(100);
		}
		
		List<Point> pointList = UtilSYT.SearchSum(a, 50);
		UtilSYT.printArr(a);
		UtilSYT.printPointArr(a, pointList);
	}
}
